package com.hcmu.networklibs.rss;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Locale;



import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;


public class RSSItem implements Comparable<RSSItem>, Parcelable {
    public String title;
    public String description;
    public String link;
    public Date pubDate;
    public String url;
    private List<String> categories;
    private String content;
    RSSFeed feed;
    
    
    //contructor
    public RSSItem() {
		// TODO Auto-generated constructor stub
	}
    
    public RSSItem(Parcel source){
    	Bundle data = source.readBundle();
		title = data.getString("title");
		link = data.getString("link");
		pubDate = (Date) data.getSerializable("pubDate");
		description = data.getString("description");
		content = data.getString("content");
		feed = data.getParcelable("feed");
    }
    
    
    public String toString() {
        return (this.title + ": " + this.pubDate + "n" + this.description);
    }

    public int compareTo(RSSItem o) {
        return (int) (o.pubDate.getTime() - pubDate.getTime());
    }

    public Date getPubDate() {
        return pubDate;
    }

    public String getDescription() {
        return description;
    }


    public String getTitle() {
        return title;
    }

    public String getLink() {
        return link;
    }
    
    public String getUrl() {
        return url;
    }
    
    public String getContent() {
		return content;
	}
    
    
    
    public List<String> getCategories() {
        if (categories == null) {
          return Collections.emptyList();
        }

        return Collections.unmodifiableList(categories);
      }

    
    public void setCategory(String category) {
        if (categories == null) {
          categories = new ArrayList<String>(3);
        }
        this.categories.add(category);
      }

    
    
    public void setFeed(RSSFeed feed) {
		this.feed = feed;
	}
    
    
    public void setTitle(String title) {
		this.title = title;
	}


	public void setLink(String link) {
		this.link = link;
	}


	public void setPubDate(Date pubDate) {
		this.pubDate = pubDate;
	}

	public void setPubDate(String pubDate) {
		try {
			SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH);
			this.pubDate = dateFormat.parse(pubDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public void setContent(String content) {
		this.content = content;
	}
    
	@Override
	public int describeContents() {
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		Bundle data = new Bundle();
		data.putString("title", title);
		data.putString("link", link);
		data.putSerializable("pubDate", pubDate);
		data.putString("description", description);
		data.putString("content", content);
		data.putParcelable("feed", (Parcelable) feed);
		dest.writeBundle(data);
	}

	public static final Parcelable.Creator<RSSItem> CREATOR = new Parcelable.Creator<RSSItem>() {
		public RSSItem createFromParcel(Parcel data) {
			return new RSSItem(data);
		}
		public RSSItem[] newArray(int size) {
			return new RSSItem[size];
		}
	};
}
